﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Write a program that reads two integer numbers N and K and an array of N elements from the console.
//Find in the array those K elements that have maximal sum.


namespace _06ArrayNMaxSumOfKElements
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("N=");
            string strArraySize = Console.ReadLine();
            int arraySize = int.Parse(strArraySize);
            kEntry:
            Console.Write("K=");
            string strSubArray = Console.ReadLine();
            int subArray = int.Parse(strSubArray);
            if (subArray > arraySize)
            {
                Console.WriteLine("K must be less than N!");
                goto kEntry;
            }
            int[] array = new int[arraySize];
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("array[{0}]", i);
                string toArray = Console.ReadLine();
                array[i] = int.Parse(toArray);
            }
            Console.WriteLine("The array you entered is this:");
            foreach (var item in array)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            int sum = 0;
            int bestSum = int.MinValue;
            string subSet = "";
            string bestSubSet = "";
            for (int i = 0; i <= array.Length - 1; i++)
            {
                if (i + subArray > array.Length)
                {
                    break;
                }
                for (int j = i; j < i + subArray; j++)
                {
                    sum += array[j];
                    subSet = subSet + " " + array[j];
                }
                if (sum > bestSum)
                {
                    bestSubSet = subSet;
                    bestSum = sum;
                }
                subSet = "";
                sum = 0;
            }
            Console.WriteLine("The subset:" + bestSubSet);
            Console.WriteLine("Has the highets sum:" + bestSum);
        }
    }
}
